<?php

class FormProcessor_SettingShop extends FormProcessor {
    
    protected $db = null;               // объект для соединения с базой данных
    public $user = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id = 0) {
        parent::__construct();
        $this->db = $db;
        $this->shop = new DatabaseObject_Shop($this->db);
        
        if ($id!='0'){
        $this->shop->load($id,'userId');
        $this->id = $this->shop->getId();
        $this->userId = $this->shop->userId;
        $this->name = $this->shop->name;
        $this->welcome = $this->shop->welcome;
        $this->refund = $this->shop->refund;
        $this->addition = $this->shop->addition;
        $this->pc2 = $this->shop->pc2;// $this->shop->pc2!='2' ? $this->shop->pc2 : ' ';
         $this->pc3 = $this->shop->pc3;
          $this->pc4 = $this->shop->pc4;
           $this->pc5 = $this->shop->pc5;
            $this->pc6 = $this->shop->pc6;
             $this->pc7 = $this->shop->pc7;
              $this->pc8 = $this->shop->pc8;
               $this->pc9 = $this->shop->pc9;
                $this->pc10 = $this->shop->pc10;
                 $this->pc21 = $this->shop->pc21;
                  $this->pc22 = $this->shop->pc22;
                   $this->pc23 = $this->shop->pc23;
                   $this->plimit = $this->shop->plimit;
                   $this->data_end = $this->shop->data_end;
                    $this->payflag = $this->shop->payflag;
                   
             
      
        }
        
    }
     public function process(Zend_Controller_Request_Abstract $request) {
        // проверим название
          $this->name = trim($request->getPost('name'));
           if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать адрес');
        else if (strlen($this->name) < 4)
            $this->addError('name', 'Адрес должен содержать больше 4 символов');
        else if ($this->shop->shopnameExists($this->name))
            $this->addError('name', 'Выбранный вами адрес уже занят');
        else
            $this->shop->name = $this->name;
        if ($request->getPost('welcome'))
            $this->shop->welcome = trim($request->getPost('welcome')); 
        if ($request->getPost('refund'))
            $this->shop->refund = trim($request->getPost('refund'));
        if ($request->getPost('addition'))
            $this->shop->addition = trim($request->getPost('addition'));
         
      
        if ($request->getPost('PC2')!='')
            $this->pc2 = $request->getPost('PC2');
        else
        if ($request->getPost('PS2'))
            $this->pc2 = 'Наличные';
        else 
            $this->pc2 = '';
        
        $this->shop->pc2 =  $this->pc2;
       
        if ($request->getPost('PC3'))
            $this->pc3 = trim($request->getPost('PC3'));
        else
        if ($request->getPost('PS3'))
            $this->pc3 = 'Банковский перевод на счёт';
        else
            $this->pc3 = '';
          $this->shop->pc3 =  $this->pc3;
          
        if ($request->getPost('PC4'))
            $this->pc4 = trim($request->getPost('PC4'));
        else
        if ($request->getPost('PS4'))
            $this->pc4 = 'Почтовый перевод';
        else
            $this->pc4 = '';
          $this->shop->pc4 =  $this->pc4;
          
        if ($request->getPost('PC5'))
            $this->pc5 = trim($request->getPost('PC5'));
        else
        if ($request->getPost('PS5'))
            $this->pc5 = 'Перевод на банковскую карту';
        else
            $this->pc5 = '';
          $this->shop->pc5 =  $this->pc5;
          
        if ($request->getPost('PC6'))
            $this->pc6 = trim($request->getPost('PC6'));
        else
        if ($request->getPost('PS6'))
            $this->pc6 = 'Электронные платёжные системы';
        else
            $this->pc6 = '';
          $this->shop->pc6 =  $this->pc6;
          
        if ($request->getPost('PC7'))
            $this->pc7 = trim($request->getPost('PC7'));
        else
        if ($request->getPost('PS7'))
            $this->pc7 = 'Система денежных переводов';
        else
            $this->pc7 = '';
        $this->shop->pc7 =  $this->pc7;
          
        if ($request->getPost('PC8'))
            $this->pc8 = trim($request->getPost('PC8'));
        else
        if ($request->getPost('PS8'))
            $this->pc8 = 'Пополнение счёта мобильного телефона';
        else
            $this->pc8 = '';
        $this->shop->pc8=  $this->pc8;
          
        if ($request->getPost('PC9'))
            $this->pc9 = trim($request->getPost('PC9'));
        else
        if ($request->getPost('PS9'))
            $this->pc9 = 'Наложенный платёж';
        else
            $this->pc9 = '';
        $this->shop->pc9 =  $this->pc9;
          
        if ($request->getPost('PC10'))
            $this->pc10 = trim($request->getPost('PC10'));
        else
            $this->pc10 = '';
        $this->shop->pc10 =  $this->pc10;
          
        if ($request->getPost('PC21'))
            $this->pc21 = trim($request->getPost('PC21'));
        else
        if ($request->getPost('PS21'))
            $this->pc21 = 'Самовывоз';
        else
            $this->pc21 = '';
          $this->shop->pc21 =  $this->pc21;
          
        if ($request->getPost('PC22'))
            $this->pc22 = trim($request->getPost('PC22'));
        else
        if ($request->getPost('PS22'))
            $this->pc22 = 'Доставка курьером';
        else
            $this->pc22 = '';
        $this->shop->pc22 =  $this->pc22;
          
            
        if ($request->getPost('PC23'))
            $this->pc23 = trim($request->getPost('PC23'));
        else
        if ($request->getPost('PS23'))
            $this->pc23 = 'Почтовая служба';
        else
            $this->pc23 = '';
        $this->shop->pc23 =  $this->pc23;
          
         
        if ($this->plimit)
            $this->shop->plimit = $this->plimit;
        else
            $this->shop->plimit = '10';
        
        if ($this->payflag)
            $this->shop->payflag = $this->payflag;
        else
            $this->shop->payflag = '0';
        
            if ($this->data_end)
            $this->shop->data_end = $this->data_end;
        else
            $this->shop->data_end = '';
        $this->shop->userId = Zend_Auth::getInstance()->getIdentity()->id;
        
           // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->shop->save();
        $this->user = new DatabaseObject_User($this->db);
        $this->user->load(Zend_Auth::getInstance()->getIdentity()->id);
        $this->user->nameshop  = $this->shop->name;
        $this->user->save();
        }
        
        return !$this->hasError();
    }
}